package com.aimuceliang.aimuceliang.ui.activity;

import android.app.AlertDialog;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.aimuceliang.aimuceliang.Constants;
import com.aimuceliang.aimuceliang.dev.R;
import com.aimuceliang.aimuceliang.green_dao.TiChiCeLiang;
import com.aimuceliang.aimuceliang.green_dao.TiChiCeLiangDaoUtils;
import com.aimuceliang.aimuceliang.ui.BaseActivity;
import com.aimuceliang.aimuceliang.ui.adapter.CommonAdapter;
import com.aimuceliang.aimuceliang.ui.adapter.CommonViewHolder;
import com.aimuceliang.aimuceliang.utils.DateUtils;
import com.aimuceliang.aimuceliang.utils.FtpManager;
import com.aimuceliang.aimuceliang.utils.JXLUtils;
import com.aimuceliang.aimuceliang.utils.XLog;

import java.io.File;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class TCDataActivity extends BaseActivity {
    @BindView(R.id.tc_num)
    EditText tcNum;
    @BindView(R.id.listView)
    ListView listView;
    private TiChiCeLiangDaoUtils daoUtils;
    private MyAdapter adapter;
    private ArrayList<TiChiCeLiang> tiChiCeLiangs;
    private AlertDialog dialog;
    public static final int UPLOAD_SUCCESS = 1;
    public static final int UPLOAD_ERROR = 2;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case UPLOAD_SUCCESS:
                    if (dialog.isShowing()) {
                        dialog.dismiss();
                    }
                    Toast.makeText(TCDataActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
                    setResult(1);
                    finish();
                    break;
                case UPLOAD_ERROR:
                    if (dialog.isShowing()) {
                        dialog.dismiss();
                    }
                    Toast.makeText(TCDataActivity.this, "上传失败", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    @Override
    protected int initContentView() {
        return R.layout.activity_tichiceliang_data;
    }

    @Override
    protected void initUIAndListener() {
        setTitle("体尺测量数据");
        daoUtils = new TiChiCeLiangDaoUtils(this);
        adapter = new MyAdapter();
        listView.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        tiChiCeLiangs = new ArrayList<>();
        tiChiCeLiangs.addAll(daoUtils.queryAllTiChiCeLiang());
        tcNum.setText(tiChiCeLiangs.size() + "条");
        adapter.bindData(tiChiCeLiangs);
    }

    @OnClick({R.id.tc_submit_btn})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.tc_submit_btn:
                initDialog();
                new Thread() {
                    @Override
                    public void run() {
                        String strExcelPath = JXLUtils.createTiChiCeLiangExcel(daoUtils);
                        if(null != strExcelPath) {
                            FtpManager ftpManager = new FtpManager();
                            try {
                                File file = new File(strExcelPath);
                                if (file.exists() && file.length() > 0) {
                                    ftpManager.connect();
                                    if (ftpManager.uploadFile(strExcelPath, Constants.SERVER_PATH)) {
                                        ftpManager.closeFTP();
                                        file.delete();//删除上传文件的本地文件

                                        // 清空数据库
                                        daoUtils.deleteAll();

                                        mHandler.sendEmptyMessage(UPLOAD_SUCCESS);
                                        XLog.d(TAG, "上传成功");
                                    } else {
                                        mHandler.sendEmptyMessage(UPLOAD_ERROR);
                                        XLog.d(TAG, "上传失败");
                                    }
                                } else {
                                    mHandler.sendEmptyMessage(UPLOAD_ERROR);
                                    XLog.d(TAG, "上传失败::文件生成出错 ");
                                }

                            } catch (Exception e) {
                                mHandler.sendEmptyMessage(UPLOAD_ERROR);
                                e.printStackTrace();
                                XLog.d(TAG, "上传失败::" + e.getMessage());
                            }
                        } else {
                            XLog.d(TAG, "上传失败::创建的文件路径为空");
                        }

                    }
                }.start();
                break;
        }
    }

    private void initDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = LayoutInflater.from(this).inflate(R.layout.layout_upload_dialog, null);
        builder.setView(view);
        builder.setCancelable(false);
        dialog = builder.create();
        dialog.show();
    }

    private class MyAdapter extends CommonAdapter<TiChiCeLiang> {

        MyAdapter() {
            super(R.layout.item_tichi_data_ap, TCDataActivity.this);
        }

        @Override
        public void convert(CommonViewHolder holder, TiChiCeLiang tiChiCeLiang) {
            //初始化点击事件,防止复用导致点击出错
            holder.setText(R.id.tc_ear_id, tiChiCeLiang.getEarid());
            holder.setText(R.id.tc_weight, tiChiCeLiang.getWeight());
            holder.setText(R.id.tc_txc, tiChiCeLiang.getTixiechang());
            holder.setText(R.id.tc_tg, tiChiCeLiang.getHeight());
        }
    }
}
